﻿using Newtonsoft.Json.Linq;
using System.ComponentModel;
using YAPACS.BusinessObjects.Managements.Exams;
using static Volo.Abp.Ui.LayoutHooks.LayoutHooks;

namespace YAPACS.RisServer.Services.ExtendCells
{
    public class FieldControlCell : ControlCell
    {
        // 规定宽度
        public static double AutoWidth
        {
            get
            {
                return Cell.TotalWidth / 2;
            }
        }

        public FieldControlCell(string key, ExamDataElement examDataElement, double width, string tag, int tableIndex) : base(key, examDataElement, width, tag, tableIndex)
        {
            if (CellType == DataElementValueType.String && examDataElement != null && !string.IsNullOrEmpty(examDataElement.ValueRange) && examDataElement.ValueRange.Contains("多行"))
            {
                IsFixedHeight = false;
            }
            ClassName = "field-container";
        }

        public JObject WrapperCustomStyle { get; set; }

        /// <summary>
        /// 无元数据
        /// </summary>
        public override void SetBodyNoDataElement()
        {
            Body = new JObject
                {
                    { "type","input-text"},
                    { "label",Key},
                    { "mode","horizontal"},
                    { "name", "report.reportExtending." + Key },
                    { "id", Guid.NewGuid().ToString() },
                    { "disabledOn","!this.report.isEditing"}
                };
            JObject horizontal = new JObject();
            horizontal.Add("leftFixed", "sm");
            Body.Add("horizontal", horizontal);
            JObject wrapperCustomStyle = new JObject();
            JObject root = new JObject();
            root.Add("margin-top", "4px");
            wrapperCustomStyle.Add("root", root);
            Body.Add("wrapperCustomStyle", wrapperCustomStyle);
            //AddRemark(Tag);
        }

        /// <summary>
        /// 有元数据
        /// </summary>
        /// <param name="key"></param>
        public override void SetBody()
        {
            Body = new JObject
            {
                { "name", "report.reportExtending." + Key },
                { "id", Guid.NewGuid().ToString() },
                { "disabledOn","!this.report.isEditing"},
                { "label",Key},
                { "mode","horizontal"}
            };
            // 添加单元格样式
            JObject horizontal = new JObject();
            horizontal.Add("leftFixed", "sm");
            Body.Add("horizontal", horizontal);
            JObject wrapperCustomStyle = new JObject();
            JObject root=new JObject();
            root.Add("margin-top", "4px");
            wrapperCustomStyle.Add("root",root);
            Body.Add("wrapperCustomStyle", wrapperCustomStyle);
            if (!string.IsNullOrEmpty(Key))
            {
                switch (CellType)
                {
                    case DataElementValueType.Boolean:
                        Body.Add("type", "checkbox");
                        break;

                    case DataElementValueType.Integer:
                        Body.Add("type", "input-number");
                        Body.Add("validateOnChange", true);
                        //AddRemark(Tag);
                        break;

                    case DataElementValueType.Enum:

                        Body.Add("type", "select");
                        if (dataElement.IsMultiValue)
                        {
                            Body.Add("multiple", true);
                            Body.Add("joinValues",true);
                        }
                        else
                        {
                            Body.Add("multiple",false);
                        }
                       
                        JArray options = [];
                        if (!string.IsNullOrEmpty(dataElement.ValueRange))
                        {
                            foreach (string enumValue in dataElement.ValueRange.Split(','))
                            {
                                JObject option = new JObject();
                                option.Add("label", enumValue);
                                option.Add("value", enumValue);
                                options.Add(option);
                            }
                        }

                        Body.Add("options", options);
                        //AddRemark(Tag);
                        break;

                    case DataElementValueType.Date:
                        Body.Add("type", "input-date");
                        Body.Add("valueFormat", "YYYY年MM月DD日");

                        break;

                    case DataElementValueType.Float:
                        Body.Add("type", "input-number");
                        Body.Add("validateOnChange", true);

                        Body.Add("precision", 2);
                        //AddRemark(Tag);

                        break;

                    case DataElementValueType.String:
                        if (dataElement.IsMultiLine)
                        {
                            if (Style["height"] != null)
                            { 
                                Style.Remove("height");
                            }
                            Body.Add("type","textarea");
                            Body.Add("minRows",1);
                            originalWidth = ControlCell.TotalWidth;
                            WidthCoculate();
                        }
                        else
                        {
                            Body.Add("type", "input-text");
                            if (!string.IsNullOrEmpty(dataElement.ValueRange))
                            {
                                JArray optionItems = new JArray();
                                string[] items = dataElement.ValueRange.Split(',');
                                foreach (var item in items)
                                {
                                    JObject optionItem = new JObject {
                                        {"label",item },
                                        { "value",item}
                                    };
                                    optionItems.Add(optionItem);
                                }
                                if (optionItems.Count > 0)
                                {
                                    Body.Add("options",optionItems);
                                }
                                if (dataElement.IsMultiValue) // 可多选
                                {
                                    Body.Add("multiple", true);
                                }
                                else
                                {
                                    Body.Add("multiple", false);
                                }
                            }
                        }
                       
                        //AddRemark(Tag);
                        break;

                    case DataElementValueType.Time:
                        Body.Add("type", "input-text");
                        //AddRemark(Tag);
                        break;

                    default:
                        break;
                }
            }
        }


        /// <summary>
        /// 获取单元格schema
        /// </summary>
        /// <returns></returns>
        public override JObject GetSchema()
        {
            JObject container = [];
            if (!string.IsNullOrEmpty(ClassName))
            {
                container.Add("className", ClassName);
            }
            container.Add("type", Type);
            container.Add("isFixedHeight", IsFixedHeight);
            if (Body != null)
            {
                container.Add("body", Body);
            }

            if (Style != null)
            {
                container.Add("style", Style);
            }

            return container;
        }
    }
}
